package com.cloudbroker.bcs.platform.ltb.middle.bizservice.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.csvc.lightinvesting.api.entity.*;
import com.cloudbroker.bcs.csvc.lightinvesting.api.t2service.LedT2Service;
import com.cloudbroker.bcs.csvc.lightinvesting.api.t2service.LedThreeT2Service;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.DataDictionaryQueryForm;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.fansRelated.*;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.interestRelated.FocusInterestForm;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.response.*;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.response.IFS.UserinfoVo;
import com.cloudbroker.bcs.platform.ltb.bizapi.constants.HSBCPSErrorCodes;
import com.cloudbroker.bcs.platform.ltb.bizapi.constants.type.UserType;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.admin.back.UserEntityVO;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.base.vo.BrokerVO;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.leader.LeaderBizService;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.leader.back.*;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.leader.vo.InviteLogForm;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.leader.vo.LeaderGetMessageForm;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.leader.vo.MiddleComuserSignQryForm;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.leader.vo.MiddleUserAdviserHisQryForm;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.transaction.vo.ComUserCollectForm;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.transaction.vo.LeaderRankForm;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.transaction.vo.LeaderRecommendForm;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.user.back.UsersQueryVo;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.user.vo.UserIdList;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.user.vo.UserListQueryForm;
import com.cloudbroker.bcs.platform.ltb.middle.entity.Ask;
import com.cloudbroker.bcs.platform.ltb.middle.entity.UserEntity;
import com.cloudbroker.bcs.platform.ltb.middle.entity.ViewpointMsg;
import com.cloudbroker.bcs.platform.ltb.middle.extservice.lightinvesting.LightinvestingService;
import com.cloudbroker.bcs.platform.ltb.middle.extservice.usercenter.UserCenterService;
import com.cloudbroker.bcs.platform.ltb.middle.model.UserCache;
import com.cloudbroker.bcs.platform.ltb.middle.service.*;
import com.cloudbroker.bcs.platform.ltb.middle.util.StringUtil;
import com.cloudbroker.bcs.platform.ltb.middle.util.UserTokenUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by tianrui on 2016/7/22.
 */

@Service
public class LeaderBizServiceImpl implements LeaderBizService {

    private final static String COMINVEST_STYLE_DICT_ENTRY = "39027";
    @Resource
    private LightinvestingService lightinvestingService;

    @Resource
    private UserService userService;
    @Resource
    private ViewpointMsgService viewpointMsgService;

    @Resource
    private LeaderService leaderService;

    @Resource
    private LedT2Service ledT2Service;

    @Resource
    private LedThreeT2Service ledThreeT2Service;

    @Resource
    private InviteLogService inviteLogService;

    @Resource
    private UserCenterService userCenterService;
    @Resource
    private AskService askService;


    @Override
    public LeaderBackVo getLeaderMessage(LeaderGetMessageForm form) {//达人信息查询
        LeaderBackVo backVo = new LeaderBackVo();
        UserEntity user = null;
        UserinfoVo userT2InfoBack=null;
        //判断是投顾请求还是粉丝请求
        if(form.getUser()!=null && form.getUser().getUser_type().equals(String.valueOf(UserType.LEADER.getVal()))){
            user = userService.get(form.getUser().getUser_id().toString());
            if(user == null){
                return VOUtil.genErrorResult(HSBCPSErrorCodes.ClientErr.USER_NOT_EXIT, LeaderBackVo.class);
            }
            UserCache userCache = UserTokenUtil.queryUserToken(form.getUser_token());
            BrokerVO brokerVO = userCache.getBrokerVO();
            backVo.setLast_login_time(brokerVO.getLast_login_time());
        }else{
            user = userService.get(form.getAdviser_id());
            if(user == null){
                return VOUtil.genErrorResult(HSBCPSErrorCodes.ClientErr.USER_NOT_EXIT, LeaderBackVo.class);
            }
            backVo.setLast_login_time(user.getLast_login_time());
        }
        InterestDetailForm ledT2Form = new InterestDetailForm();
        ledT2Form.setSendercomp_id(user.getSendercomp_id());
        ledT2Form.setUser_id(String.valueOf(user.getUser_id()));
        ledT2Form.setUser_token(form.getUser_token());
        ledT2Form.setNeed_user_id(form.getNeed_user_id());
        InterestDetailVo interestDetailVo = lightinvestingService.func_interest_detail_query(ledT2Form);
//        InterestDetailVo interestDetailVo = new InterestDetailVo();
        if (StringUtils.isNotEmpty(interestDetailVo.getError_no()) && !interestDetailVo.getError_no().equals("0")){
            return VOUtil.genBGErrorResult(interestDetailVo.getError_no(),
                    interestDetailVo.getError_info(), LeaderBackVo.class);
        }


        if(form.getUser()!=null && form.getUser().getUser_type().equals(String.valueOf(UserType.CUSTOMER.getVal()))){
            //查询是否已收藏
            FocusInterestForm focusInterestForm = new FocusInterestForm();
            focusInterestForm.setOp_branch_no(form.getOp_branch_no());
            focusInterestForm.setOp_entrust_way(form.getOp_entrust_way());
            focusInterestForm.setOp_station(form.getOp_station());
            focusInterestForm.setBranch_no(form.getBranch_no());
            focusInterestForm.setPassword(form.getPassword());
            focusInterestForm.setPassword_type(form.getPassword_type());
            focusInterestForm.setUser_token(form.getUser_token());
            focusInterestForm.setSendercomp_id(form.getSendercomp_id());
            if (form.getUser()!=null) {
                focusInterestForm.setUser_id(String.valueOf(form.getUser().getUser_id()));
            }
            focusInterestForm.setNeed_user_id(form.getNeed_user_id());
            FocusInterestQryBackVo focusInterestQryBackVo = ledT2Service.func_interest_focus(focusInterestForm);
            List<FocusInterestResult> list = focusInterestQryBackVo.getData_list();
            if(list == null || list.size() == 0){
                backVo.setIs_collect(0);
            }else {
                for (FocusInterestResult result : list) {
                    if (Long.valueOf(result.getAdviser_id()).equals(user.getUser_id())) {
                        backVo.setIs_collect(1);
                        break;
                    } else {
                        backVo.setIs_collect(0);
                    }
                }
            }
            //查询是否已签约
            ComuserSignQryForm signForm = new ComuserSignQryForm();
            signForm.setSendercomp_id(form.getUser().getSendercomp_id());
            signForm.setUser_id(String.valueOf(form.getUser().getUser_id()));
            signForm.setUser_token(form.getUser_token());
            signForm.setNeed_user_id(form.getNeed_user_id());
            ComuserSignQryBackVo comuserSignQryBackVo = lightinvestingService.func_comuser_sign_qry(signForm);
            List<ComuserSignQry> signList = comuserSignQryBackVo.getData_list();
            if(signList == null || signList.size() == 0){
                backVo.setIs_sign(0);
            }else {
                for (ComuserSignQry sign : signList) {
                    if (Long.valueOf(sign.getAdviser_id()).equals(user.getUser_id())) {
                        backVo.setIs_sign(1);
                        break;
                    } else {
                        backVo.setIs_sign(0);
                    }
                }
            }
        }

        UsersQueryVo usersQueryVo = userCenterService.func_users_query(user.getUser_id()+"");
        List<UserEntityVO> userList = usersQueryVo.getData_list();
        if(!userList.isEmpty()){
            //暂时直接从用户中心取数据
            UserEntityVO userEntityVO = userList.get(0);
            backVo.setAlias_name(StringUtil.replaceMobile(userEntityVO.getAlias_name()));//修改单号M201701170424
            backVo.setAvatar(userEntityVO.getAvatar());
        }else{
            backVo.setAlias_name(StringUtil.replaceMobile(user.getAlias_name()));//修改单号M201701170424
            backVo.setAvatar(user.getAvatar());
        }

        ViewpointMsg viewpointMsg = new ViewpointMsg();
        viewpointMsg.setUser_id(user.getUser_id());
        viewpointMsg.setCompany_id(user.getCompany_id());
        backVo.setView_point_num(String.valueOf(viewpointMsgService.count(viewpointMsg)));
        backVo.setInvite_code(user.getInvite_code());
        backVo.setInvite_num(user.getInvite_num());

//        backVo.setAlias_name(user.getAlias_name());
//        backVo.setAvatar(user.getAvatar());
        backVo.setAvatar_base64(user.getAvatar_base64());
        backVo.setCompany_id(user.getCompany_id());
        backVo.setSendercomp_id(user.getSendercomp_id());

        backVo.setMobile(user.getMobile());
        backVo.setAnswer_num(user.getAnswer_num());//总回答数
        backVo.setAdviser_type(interestDetailVo.getAdviser_type());

        backVo.setCollect_num(interestDetailVo.getCollect_num());
        backVo.setSuccess_rate(interestDetailVo.getSuccess_rate());
        backVo.setClient_branch_no(interestDetailVo.getClient_branch_no());
        backVo.setBranch_name(interestDetailVo.getBranch_name());
        backVo.setClient_name(interestDetailVo.getClient_name());
        backVo.setQualify_no(interestDetailVo.getQualify_no());
        backVo.setAnnual_income_ratio(interestDetailVo.getAnnual_income_ratio());
        backVo.setUser_type(interestDetailVo.getUser_type());
        backVo.setFollow_num(interestDetailVo.getFollow_num());
        backVo.setCollect_num(interestDetailVo.getCollect_num());
        backVo.setSign_num(interestDetailVo.getSign_num());
        backVo.setSuccess_rate(interestDetailVo.getSuccess_rate());
        backVo.setWork_age(interestDetailVo.getWork_age());
        backVo.setIncome_ratio(interestDetailVo.getIncome_ratio());
        backVo.setToday_income_ratio(interestDetailVo.getToday_income_ratio());
        backVo.setWeek_income_ratio(interestDetailVo.getWeek_income_ratio());
        backVo.setMonth_income_ratio(interestDetailVo.getMonth_income_ratio());
        backVo.setPromotion_flag(interestDetailVo.getPromotion_flag());
        backVo.setCombi_num(interestDetailVo.getCombi_num());
        backVo.setCollect_cancel_num(interestDetailVo.getCollect_cancel_num());
        backVo.setSign_cancel_num(interestDetailVo.getSign_cancel_num());
        backVo.setProfit_balance(interestDetailVo.getProfit_balance());
        backVo.setBackground_info(interestDetailVo.getBackground_info());
        backVo.setClient_branch_no(interestDetailVo.getClient_branch_no());
        backVo.setBranch_name(interestDetailVo.getBranch_name());
        backVo.setClient_name(interestDetailVo.getClient_name());
        backVo.setQualify_no(interestDetailVo.getQualify_no());
        backVo.setAnnual_income_ratio(interestDetailVo.getAnnual_income_ratio());
        backVo.setReward_num(interestDetailVo.getReward_num());
        backVo.setSuccess_num(interestDetailVo.getSuccess_num());
        backVo.setFail_num(interestDetailVo.getFail_num());
        backVo.setAddress(interestDetailVo.getAddress());
        backVo.setCombi_code(interestDetailVo.getCombi_code());
        backVo.setCombi_name(interestDetailVo.getCombi_name());
        backVo.setEn_cominvest_style(interestDetailVo.getEn_cominvest_style());
        backVo.setInvest_strategy(interestDetailVo.getInvest_strategy());
        backVo.setInvest_strategy_oview(interestDetailVo.getInvest_strategy_oview());
        backVo.setUser_status(interestDetailVo.getUser_status());
        backVo.setSex(interestDetailVo.getSex());
        backVo.setId_no(interestDetailVo.getId_no());
        backVo.setId_beginDate(interestDetailVo.getId_beginDate());
        backVo.setId_endDate(interestDetailVo.getId_endDate());
        backVo.setQq(interestDetailVo.getQq());
        backVo.setQualify_beginDate(interestDetailVo.getQualify_beginDate());
        backVo.setQualify_endDate(interestDetailVo.getQualify_endDate());
        backVo.setMobile(interestDetailVo.getMobile_tel());

//        backVo.setCompany_id(interestDetailVo.getCompany_id());
        backVo.setSex(interestDetailVo.getSex());
        backVo.setId_kind(interestDetailVo.getId_kind());
        backVo.setId_no(interestDetailVo.getId_no());
        backVo.setId_beginDate(interestDetailVo.getId_beginDate());
        backVo.setId_endDate(interestDetailVo.getId_endDate());
        backVo.setQq(interestDetailVo.getQq());
        backVo.setQualify_beginDate(interestDetailVo.getQualify_beginDate());
        backVo.setQualify_endDate(interestDetailVo.getQualify_endDate());
        backVo.setMobile_tel(interestDetailVo.getMobile_tel());
        setCominvestStyleName(backVo,form.getUser_token());
        return VOUtil.setSuccessResult(backVo);
    }

    private void setCominvestStyleName(LeaderBackVo leaderBackVo,String user_token) {
        String en_cominvest_style = leaderBackVo.getEn_cominvest_style();
        StringBuilder sb = new StringBuilder();
        Map<String,String> styleDictMap = new HashMap<>();
        DataDictionaryQueryForm form = new DataDictionaryQueryForm();
        form.setBranch_no(8888);
        form.setDict_entry(COMINVEST_STYLE_DICT_ENTRY);
        form.setUser_token(user_token);
        DataDictionaryBackVO dataDictionaryBackVO = ledThreeT2Service.dataDictionaryQuery(form);
        if (dataDictionaryBackVO.getData_list()!=null ) {
            for(DataDictionaryResult result :dataDictionaryBackVO.getData_list()){
                styleDictMap.put(result.getSubentry(),result.getDict_prompt());
            }
        }
        String cominvest_style_name = "";
        if (en_cominvest_style != null && StringUtils.isNotBlank(en_cominvest_style)) {
            String[] styleList = en_cominvest_style.split(",");
            for(int i=0; i<styleList.length; i++) {
                String styleName = styleDictMap.get(styleList[i])==null?"":styleDictMap.get(styleList[i]);
                sb.append(styleName+",");
            }
            cominvest_style_name = sb.substring(0,sb.length()-1);
        }
        leaderBackVo.setEn_cominvest_style_name(cominvest_style_name);
    }

    @Override
    public RecommendInterestQryBackVo comuser_recommend_qry(LeaderRecommendForm form) {
        RecommendInterestForm recommendInterestForm = new RecommendInterestForm();
        recommendInterestForm.setOp_branch_no(form.getOp_branch_no());
        recommendInterestForm.setOp_entrust_way(form.getOp_entrust_way());
        recommendInterestForm.setOp_station(form.getOp_station());
        recommendInterestForm.setBranch_no(form.getBranch_no());
        recommendInterestForm.setPassword(form.getPassword());
        recommendInterestForm.setPassword_type(form.getPassword_type());
        recommendInterestForm.setUser_token(form.getUser_token());
        recommendInterestForm.setSendercomp_id(form.getSendercomp_id());
        recommendInterestForm.setRecommend_flag(form.getRecommend_flag());
        recommendInterestForm.setRequest_num(form.getRequest_num());
        recommendInterestForm.setPosition_str(form.getPosition_str());
        RecommendInterestQryBackVo recommendInterestQryBackVo = ledT2Service.func_recommend_interest_query(recommendInterestForm);
        List<RecommendInterestResult> recommendList = recommendInterestQryBackVo.getData_list();
        if(recommendList == null){
            return VOUtil.setSuccessResult(recommendInterestQryBackVo);
        }
//        List<RecommendInterestResult> recommendList = new ArrayList<>();
//        RecommendInterestResult temp = new RecommendInterestResult();
//        temp.setUser_id("31");
//        recommendList.add(temp);
        //查询id列表
        List<Long> list = new ArrayList<>(recommendList.size());
        for(RecommendInterestResult recommendInterestResult : recommendList){
            list.add(Long.valueOf(recommendInterestResult.getUser_id()));
        }
        //查询结果集转成map
        Map<Long,UserEntityVO> map = this.leaderAvatarMap(list);
        //设置头像字段
        List<RecommendInterestResult> resultList = new ArrayList<>(recommendList.size());
        for(RecommendInterestResult recommend : recommendList){
            UserEntityVO avatarUser = map.get(Long.valueOf(recommend.getUser_id()));
            if(avatarUser!=null){
                recommend.setLeader_avatar(avatarUser.getAvatar());
                recommend.setAlias_name(avatarUser.getAlias_name());
            }
            resultList.add(recommend);
        }
        recommendInterestQryBackVo.setData_list(resultList);
        return recommendInterestQryBackVo;
    }

    @Override
    public InterestRankQryBackVo interest_rank_query(LeaderRankForm form) {
        InterestRankForm interestRankForm = new InterestRankForm();
        interestRankForm.setOp_branch_no(form.getOp_branch_no());
        interestRankForm.setOp_entrust_way(form.getOp_entrust_way());
        interestRankForm.setOp_station(form.getOp_station());
        interestRankForm.setPassword(form.getPassword());
        interestRankForm.setPassword_type(form.getPassword_type());
        interestRankForm.setUser_token(form.getUser_token());
        interestRankForm.setSendercomp_id(form.getSendercomp_id());
        interestRankForm.setCompany_id(form.getCompany_id());
        interestRankForm.setComorder_type(form.getComorder_type());
        interestRankForm.setPosition_str(form.getPosition_str());
        interestRankForm.setRequest_num(form.getRequest_num());
        interestRankForm.setMobile_tel(form.getMobile_tel());
        interestRankForm.setAlias_name(form.getAlias_name());
        interestRankForm.setAdviser_type(form.getAdviser_type());
        interestRankForm.setBranch_name(form.getBranch_name());
        if(form.getUser()!=null){
            interestRankForm.setUser_id(String.valueOf(form.getUser().getUser_id()));
        }
        InterestRankQryBackVo interestRankQryBackVo = ledT2Service.func_interest_rank_query(interestRankForm);
        List<InterestRankResult> list = interestRankQryBackVo.getData_list();
        if(list == null){
            return VOUtil.setSuccessResult(interestRankQryBackVo);
        }
        List<Long> idList = new ArrayList<>(list.size());
        for(InterestRankResult result : list){
            idList.add(Long.valueOf(result.getUser_id()));
        }
        Map<Long,UserEntityVO> avatarMap = this.leaderAvatarMap(idList);
        Map<Long,UserEntity> userMap = this.userQryByIdList(idList);
        Map<Long,Ask> answerMap = new HashMap<>();
        if(idList.size()>0){
            answerMap = this.getLastAnswer(idList);
        }
        List<InterestRankResult> backList = new ArrayList<>(list.size());
        for(InterestRankResult result : list){
            UserEntityVO avatarUser = avatarMap.get(Long.valueOf(result.getUser_id()));
            if(avatarUser!=null){
                result.setLeader_avatar(avatarUser.getAvatar());
                result.setAlias_name(avatarUser.getAlias_name());
            }
            UserEntity userEntity = userMap.get(Long.valueOf(result.getUser_id()));
            if(userEntity!=null){
                result.setAnswer_num(userEntity.getAnswer_num());
            }
            Ask answer = answerMap.get(Long.valueOf(result.getUser_id()));
            if(answer!=null){
                result.setLast_answer_time(answer.getCreate_time());
            }
            backList.add(result);
        }
        interestRankQryBackVo.setData_list(backList);
        return interestRankQryBackVo;
    }

    @Override
    public FocusInterestQryBackVo interest_focus(ComUserCollectForm form) {
        FocusInterestForm focusInterestForm = new FocusInterestForm();
        focusInterestForm.setPosition_str(form.getPosition_str());
        focusInterestForm.setRequest_num(form.getRequest_num());
        focusInterestForm.setOp_branch_no(form.getOp_branch_no());
        focusInterestForm.setOp_entrust_way(form.getOp_entrust_way());
        focusInterestForm.setOp_station(form.getOp_station());
        focusInterestForm.setBranch_no(form.getBranch_no());
        focusInterestForm.setPassword(form.getPassword());
        focusInterestForm.setPassword_type(form.getPassword_type());
        focusInterestForm.setUser_token(form.getUser_token());
        focusInterestForm.setSendercomp_id(form.getSendercomp_id());
        if (form.getUser()!=null) {
            focusInterestForm.setUser_id(String.valueOf(form.getUser().getUser_id()));
        }
        focusInterestForm.setPosition_str(form.getPosition_str());
        focusInterestForm.setRequest_num(form.getRequest_num());
        focusInterestForm.setNeed_user_id(form.getNeed_user_id());

        FocusInterestQryBackVo focusInterestQryBackVo = ledT2Service.func_interest_focus(focusInterestForm);
        List<FocusInterestResult> list = focusInterestQryBackVo.getData_list();
        if (list == null) {
            return VOUtil.setSuccessResult(focusInterestQryBackVo);
        }
        List<Long> idList = new ArrayList<>(list.size());
        for(FocusInterestResult result : list){
            idList.add(Long.valueOf(result.getAdviser_id()));
        }
        Map<Long,UserEntityVO> aratarMap = this.leaderAvatarMap(idList);
        Map<Long,UserEntity> userMap = this.userQryByIdList(idList);
        Map<Long,Ask> answerMap = new HashMap<>();
        if(idList.size()>0){
            answerMap = this.getLastAnswer(idList);
        }
        List<FocusInterestResult> resultList = new ArrayList<>(list.size());
        for(FocusInterestResult result : list){
            UserEntityVO userEntityVO = aratarMap.get(Long.valueOf(result.getAdviser_id()));
            if(userEntityVO!=null){
                result.setLeader_avatar(userEntityVO.getAvatar());
                result.setAlias_name(userEntityVO.getAlias_name());
            }
            UserEntity userEntity = userMap.get(Long.valueOf(result.getAdviser_id()));
            if(userEntity!=null){
                result.setAnswer_num(userEntity.getAnswer_num());
            }
            Ask answer = answerMap.get(Long.valueOf(Long.valueOf(result.getAdviser_id())));
            if(answer!=null){
                result.setLast_answer_time(answer.getCreate_time());
            }
            resultList.add(result);
        }
        focusInterestQryBackVo.setData_list(resultList);
        return focusInterestQryBackVo;
    }

    private Map<Long,UserEntity> userQryByIdList(List<Long> list){
        List<UserEntity>  userList = userService.getUserList(list);
        Map<Long,UserEntity> map = new HashMap<>(userList.size());
        for(UserEntity user : userList){
            map.put(user.getUser_id(),user);
        }
        return map;
    }

    private Map<Long,UserEntityVO> leaderAvatarMap(List<Long> list){
        UsersQueryVo usersQueryVo = userCenterService.func_users_query(org.apache.commons.lang3.StringUtils.join(list, ","));
        //查询结果集转成map
        List<UserEntityVO> userList = usersQueryVo.getData_list();
        Map<Long,UserEntityVO> map = new HashMap<>(userList.size());
        for(UserEntityVO user : userList){
            user.setAlias_name(StringUtil.replaceMobile(user.getAlias_name()));//修改单号M201701170424
            map.put(user.getUser_id(),user);
        }
        return map;
    }

    private Map<Long,Ask> getLastAnswer(List<Long> list){
        List<Ask> askList = askService.getLastAnswerByLeaderId(list);
        Map<Long,Ask> map = new HashMap<>(askList.size());
        for(Ask ask : askList){
            map.put(ask.getLeader_id(),ask);
        }
        return map;
    }


    @Override
    public LeaderListBackVo getLeaderList(UserListQueryForm form) {
        List<UserIdList> list = JSONArray.parseArray(form.getData_list(),UserIdList.class);
        List<Long> idList = new ArrayList<>(list.size());
        for(UserIdList userIdList : list){
            idList.add(userIdList.getUser_id());
        }
        UsersQueryVo usersQueryVo = userCenterService.func_users_query(org.apache.commons.lang3.StringUtils.join(list.toArray(), ","));
        if(!VOUtil.isSuccess(usersQueryVo)){
            return VOUtil.genBGErrorResult(usersQueryVo.getError_no(),usersQueryVo.getError_info(),LeaderListBackVo.class);
        }
        //查询结果集转成map
        List<UserEntityVO> userList = usersQueryVo.getData_list();
        if(userList == null || userList.size() == 0){
            return VOUtil.setSuccessResult(new LeaderListBackVo());
        }
//        List<UserEntity> userList = userService.getUserList(idList);
        List<LeaderListBackData> backList = new ArrayList<>(list.size());
        for(UserEntityVO user : userList){
            user.setAlias_name(StringUtil.replaceMobile(user.getAlias_name()));//修改单号M201701170424
            InterestDetailForm ledT2Form = new InterestDetailForm();
            ledT2Form.setSendercomp_id(form.getSendercomp_id());
            ledT2Form.setUser_id(String.valueOf(user.getUser_id()));
            ledT2Form.setUser_token(form.getUser_token());
            ledT2Form.setNeed_user_id(form.getNeed_user_id());
//            InterestDetailVo interestDetailVo = new InterestDetailVo();
            InterestDetailVo interestDetailVo = lightinvestingService.func_interest_detail_query(ledT2Form);
            if (StringUtils.isNotEmpty(interestDetailVo.getError_no()) && !interestDetailVo.getError_no().equals("0")){
                return VOUtil.genBGErrorResult(interestDetailVo.getError_no(),
                        interestDetailVo.getError_info(), LeaderListBackVo.class);
            }
            LeaderListBackData backVo = new LeaderListBackData();
            ViewpointMsg viewpointMsg = new ViewpointMsg();
            viewpointMsg.setUser_id(user.getUser_id());
            viewpointMsg.setCompany_id(form.getCompany_id());
            backVo.setView_point_num(String.valueOf(viewpointMsgService.count(viewpointMsg)));
            backVo.setAlias_name(user.getAlias_name());
            backVo.setAvatar(user.getAvatar());
            backVo.setCompany_id(user.getCompany_id());
//            UserCache userCache = UserTokenUtil.queryUserToken(form.getUser_token());
//            BrokerVO brokerVO = userCache.getBrokerVO();
//            backVo.setLast_login_time(brokerVO.getLast_login_time());
            backVo.setMobile(user.getMobile());
            backVo.setAdviser_type(interestDetailVo.getAdviser_type());
            backVo.setCollect_num(interestDetailVo.getCollect_num());
            backVo.setSuccess_rate(interestDetailVo.getSuccess_rate());
            backVo.setClient_branch_no(interestDetailVo.getClient_branch_no());
            backVo.setBranch_name(interestDetailVo.getBranch_name());
            backVo.setClient_name(interestDetailVo.getClient_name());
            backVo.setQualify_no(interestDetailVo.getQualify_no());
            backVo.setAnnual_income_ratio(interestDetailVo.getAnnual_income_ratio());
            backVo.setUser_type(interestDetailVo.getUser_type());
            backVo.setFollow_num(interestDetailVo.getFollow_num());
            backVo.setCollect_num(interestDetailVo.getCollect_num());
            backVo.setSign_num(interestDetailVo.getSign_num());
            backVo.setSuccess_rate(interestDetailVo.getSuccess_rate());
            backVo.setWork_age(interestDetailVo.getWork_age());
            backVo.setIncome_ratio(interestDetailVo.getIncome_ratio());
            backVo.setToday_income_ratio(interestDetailVo.getToday_income_ratio());
            backVo.setWeek_income_ratio(interestDetailVo.getWeek_income_ratio());
            backVo.setMonth_income_ratio(interestDetailVo.getMonth_income_ratio());
            backVo.setPromotion_flag(interestDetailVo.getPromotion_flag());
            backVo.setCombi_num(interestDetailVo.getCombi_num());
            backVo.setCollect_cancel_num(interestDetailVo.getCollect_cancel_num());
            backVo.setSign_cancel_num(interestDetailVo.getSign_cancel_num());
            backVo.setProfit_balance(interestDetailVo.getProfit_balance());
            backVo.setBackground_info(interestDetailVo.getBackground_info());
            backVo.setClient_branch_no(interestDetailVo.getClient_branch_no());
            backVo.setBranch_name(interestDetailVo.getBranch_name());
            backVo.setClient_name(interestDetailVo.getClient_name());
            backVo.setQualify_no(interestDetailVo.getQualify_no());
            backVo.setAnnual_income_ratio(interestDetailVo.getAnnual_income_ratio());
            backVo.setReward_num(interestDetailVo.getReward_num());
            backVo.setSuccess_num(interestDetailVo.getSuccess_num());
            backVo.setFail_num(interestDetailVo.getFail_num());
            backVo.setAddress(interestDetailVo.getAddress());
            backVo.setAdviser_id(user.getUser_id());
            backList.add(backVo);
        }
        LeaderListBackVo back = new LeaderListBackVo();
        back.setData_list(backList);
        return VOUtil.setSuccessResult(back);
    }

    @Override
    public UserAdviserHisQryVo user_adviser_his_qry(MiddleUserAdviserHisQryForm form) {
        UserAdviserHisQryForm userAdviserHisQryForm = new UserAdviserHisQryForm();
        userAdviserHisQryForm.setUser_token(form.getUser_token());
        userAdviserHisQryForm.setSendercomp_id(form.getSendercomp_id());
        userAdviserHisQryForm.setNeed_user_id(form.getNeed_user_id());
        if(form.getUser()!=null){
            userAdviserHisQryForm.setUser_id(String.valueOf(form.getUser().getUser_id()));
        }
        UserAdviserHisQryVo userAdviserHisQryVo = ledT2Service.func_user_adviser_his_qry(userAdviserHisQryForm);
        //获取用户已收藏投顾
        FocusInterestForm ledT2Form = new FocusInterestForm();
        ledT2Form.setSendercomp_id(form.getUser().getSendercomp_id());
        ledT2Form.setUser_id(String.valueOf(form.getUser_id()));
        ledT2Form.setUser_token(form.getUser_token());
        ledT2Form.setNeed_user_id(form.getNeed_user_id());
        if(form.getUser()!=null){
            ledT2Form.setUser_id(String.valueOf(form.getUser().getUser_id()));
        }
        FocusInterestQryBackVo focusInterestQryBackVo = ledT2Service.func_interest_focus(ledT2Form);
        if (StringUtils.isNotEmpty(focusInterestQryBackVo.getError_no()) && !"0".equals(focusInterestQryBackVo.getError_no())){
            return VOUtil.genBGErrorResult(focusInterestQryBackVo.getError_no(),
                    focusInterestQryBackVo.getError_info(), UserAdviserHisQryVo.class);
        }
        List<String> userCollectList = new ArrayList<>();
        if (focusInterestQryBackVo.getData_list()==null || focusInterestQryBackVo.getData_list().size()==0) {
            userCollectList = Collections.EMPTY_LIST;
        }else {
            for (FocusInterestResult focusInterestResult:focusInterestQryBackVo.getData_list()){
                userCollectList.add(focusInterestResult.getAdviser_id());
            }
        }
        List<UserAdviserHisQryResult> list = userAdviserHisQryVo.getData_list();
        if (list !=null && list.size()>0) {
            List<Long> idList = new ArrayList<>(list.size());
            for (UserAdviserHisQryResult result : list) {
                idList.add(Long.valueOf(result.getAdviser_id()));
            }
            Map<Long, UserEntityVO> map = this.leaderAvatarMap(idList);
            List<UserAdviserHisQryResult> resultList = new ArrayList<>(list.size());
            for (UserAdviserHisQryResult result : list) {
                UserEntityVO avatarUser = map.get(Long.valueOf(result.getAdviser_id()));
                if (avatarUser != null) {
                    result.setLeader_avatar(avatarUser.getAvatar());
                }
                //设置签约标志
                if (userCollectList.contains(result.getAdviser_id())){
                    result.setCollect_flag("1");
                }else {
                    result.setCollect_flag("0");
                }
                resultList.add(result);
            }
            userAdviserHisQryVo.setData_list(resultList);
        }
        return userAdviserHisQryVo;
    }

    public InviteLogList fetchInviteLogs(InviteLogForm form){
        Map<String,Object> paramMap = JSON.parseObject(JSON.toJSONString(form),new TypeReference<Map<String, Object>>(){});
        paramMap.put("inviter_id",form.getUser().getUser_id());
        paramMap.put("mobile",form.getMobile());
        paramMap.put("alias_name",form.getAlias_name());
        paramMap.put("page_no",(form.getPage_no()-1)*form.getRequest_num());

        List<Map> result= inviteLogService.fetchInviteLogs(paramMap);
        List<InviteLogBackVo> resultList = JSON.parseObject(JSON.toJSONString(result),new TypeReference<List<InviteLogBackVo>>(){});
        for(InviteLogBackVo backVo:resultList){
            backVo.setMobile(backVo.getMobile().replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2"));
        }
        int item_count = inviteLogService.count(paramMap);
        InviteLogList list = new InviteLogList();
        list.setData_list(resultList);
        list.setTotal_item_num(item_count);
        list.setPage_no(form.getPage_no());
        list.setPage_size(form.getRequest_num());
        return VOUtil.setSuccessResult(list);
    }

    public ComuserSignQryList func_comuser_sign_qry(MiddleComuserSignQryForm form){//848535 用户已签约达人查询
        ComuserSignQryList comuserSignQryList = new ComuserSignQryList();
        List<ComuserSignQryBackData> result_list = new ArrayList<>();
        ComuserSignQryForm ledT2Form = new ComuserSignQryForm();
        ledT2Form.setSendercomp_id(form.getUser().getSendercomp_id());
        ledT2Form.setUser_id(String.valueOf(form.getUser().getUser_id()));
        ledT2Form.setUser_token(form.getUser_token());
        ledT2Form.setNeed_user_id(form.getNeed_user_id());
        ComuserSignQryBackVo comuserSignQryBackVo = lightinvestingService.func_comuser_sign_qry(ledT2Form);
        if (StringUtils.isNotEmpty(comuserSignQryBackVo.getError_no()) && !"0".equals(comuserSignQryBackVo.getError_no())){
            return VOUtil.genBGErrorResult(comuserSignQryBackVo.getError_no(),
                    comuserSignQryBackVo.getError_info(), ComuserSignQryList.class);
        }
        List<ComuserSignQry> list = comuserSignQryBackVo.getData_list();

        //获取投顾头像,生成map
        if (list == null) {
            return VOUtil.setSuccessResult(comuserSignQryList);
        }
        List<Long> idList = new ArrayList<>(list.size());
        for(ComuserSignQry result : list){
            idList.add(Long.valueOf(result.getAdviser_id()));
        }
        Map<Long,UserEntityVO> map = this.leaderAvatarMap(idList);

        for (ComuserSignQry comuserSignQry : list) {
            ComuserSignQryBackData comuserSignQryBackData = new ComuserSignQryBackData();
            BeanUtils.copyProperties(comuserSignQry,comuserSignQryBackData);
            //添加头像
            UserEntityVO userEntity = map.get(Long.valueOf(comuserSignQry.getAdviser_id()));
            if(userEntity!=null){
                comuserSignQryBackData.setLeader_avatar(userEntity.getAvatar());
            }
            result_list.add(comuserSignQryBackData);
        }
        comuserSignQryList.setData_list(result_list);
        return VOUtil.setSuccessResult(comuserSignQryList);
    }

}
